<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">mongoose</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">require</span><span class="PUNC">(</span><span class="STRN">'mongoose'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>  2</span> </span><span class="NAME">Schema</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">mongoose.Schema</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>  3</span> 
<span class='line'>  4</span> </span><span class="NAME">mongoose.connect</span><span class="PUNC">(</span><span class="STRN">'mongodb://127.0.0.1/test1'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>  5</span> 
<span class='line'>  6</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">PersonSchema</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">Schema</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>  7</span> </span><span class="WHIT">	</span><span class="NAME">name</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">String</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>  8</span> </span><span class="WHIT">	</span><span class="NAME">age</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">Number</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>  9</span> </span><span class="WHIT">	</span><span class="NAME">stories</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 10</span> </span><span class="WHIT">		</span><span class="NAME">type</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">Schema.ObjectId</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="WHIT">		</span><span class="NAME">ref</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">'Story'</span><span class="WHIT">
<span class='line'> 12</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT"> </span><span class="PUNC">]</span><span class="WHIT">
<span class='line'> 13</span> </span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 14</span> 
<span class='line'> 15</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">StorySchema</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">Schema</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 16</span> </span><span class="WHIT">	</span><span class="NAME">_creator</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 17</span> </span><span class="WHIT">		</span><span class="NAME">type</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">Schema.ObjectId</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 18</span> </span><span class="WHIT">		</span><span class="NAME">ref</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">'Person'</span><span class="WHIT">
<span class='line'> 19</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 20</span> </span><span class="WHIT">	</span><span class="NAME">title</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">String</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 21</span> </span><span class="WHIT">	</span><span class="NAME">fans</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 22</span> </span><span class="WHIT">		</span><span class="NAME">type</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">Schema.ObjectId</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 23</span> </span><span class="WHIT">		</span><span class="NAME">ref</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">'Person'</span><span class="WHIT">
<span class='line'> 24</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="WHIT"> </span><span class="PUNC">]</span><span class="WHIT">
<span class='line'> 25</span> </span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 26</span> 
<span class='line'> 27</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">Story</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">mongoose.model</span><span class="PUNC">(</span><span class="STRN">'Story'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">StorySchema</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 28</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">Person</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">mongoose.model</span><span class="PUNC">(</span><span class="STRN">'Person'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">PersonSchema</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 29</span> 
<span class='line'> 30</span> </span><span class="COMM">// var aaron = new Person({</span><span class="WHIT">
<span class='line'> 31</span> </span><span class="COMM">// name : 'Aaron',</span><span class="WHIT">
<span class='line'> 32</span> </span><span class="COMM">// age : 100</span><span class="WHIT">
<span class='line'> 33</span> </span><span class="COMM">// });</span><span class="WHIT">
<span class='line'> 34</span> </span><span class="COMM">// aaron.save(function(err) {</span><span class="WHIT">
<span class='line'> 35</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='line'> 36</span> </span><span class="COMM">// var story1 = new Story({</span><span class="WHIT">
<span class='line'> 37</span> </span><span class="COMM">// title : "A man who cooked Nintendo",</span><span class="WHIT">
<span class='line'> 38</span> </span><span class="COMM">// _creator : aaron._id</span><span class="WHIT">
<span class='line'> 39</span> </span><span class="COMM">// });</span><span class="WHIT">
<span class='line'> 40</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='line'> 41</span> </span><span class="COMM">// story1.save(function(err) {</span><span class="WHIT">
<span class='line'> 42</span> </span><span class="COMM">// });</span><span class="WHIT">
<span class='line'> 43</span> </span><span class="COMM">// });</span><span class="WHIT">
<span class='line'> 44</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='line'> 45</span> </span><span class="COMM">// Story.findOne({</span><span class="WHIT">
<span class='line'> 46</span> </span><span class="COMM">// title : /Nintendo/i</span><span class="WHIT">
<span class='line'> 47</span> </span><span class="COMM">// }).populate('_creator').run(function(err, story) {</span><span class="WHIT">
<span class='line'> 48</span> </span><span class="COMM">// console.log('The creator is ' + story._creator.name);</span><span class="WHIT">
<span class='line'> 49</span> </span><span class="COMM">// });</span><span class="WHIT">
<span class='line'> 50</span> 
<span class='line'> 51</span> </span><span class="COMM">// Story</span><span class="WHIT">
<span class='line'> 52</span> </span><span class="COMM">// .findOne({ title: /Nintendo/i })</span><span class="WHIT">
<span class='line'> 53</span> </span><span class="COMM">// .populate('_creator', ['name']) // &lt;-- only return the Persons name</span><span class="WHIT">
<span class='line'> 54</span> </span><span class="COMM">// .run(function (err, story) {</span><span class="WHIT">
<span class='line'> 55</span> </span><span class="COMM">//	</span><span class="WHIT">
<span class='line'> 56</span> </span><span class="COMM">// console.log('The creator is %s', story._creator.name);</span><span class="WHIT">
<span class='line'> 57</span> </span><span class="COMM">// // prints "The creator is Aaron"</span><span class="WHIT">
<span class='line'> 58</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='line'> 59</span> </span><span class="COMM">// console.log('The creators age is %s', story._creator.age);</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="COMM">// // prints "The creators age is null'</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="COMM">// });</span><span class="WHIT">
<span class='line'> 62</span> 
<span class='line'> 63</span> </span><span class="COMM">//var guille = new Person({</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="COMM">//	name : 'Guillermo'</span><span class="WHIT">
<span class='line'> 65</span> </span><span class="COMM">//});</span><span class="WHIT">
<span class='line'> 66</span> 
<span class='line'> 67</span> </span><span class="COMM">//guille.save(function(err) {</span><span class="WHIT">
<span class='line'> 68</span> </span><span class="COMM">//</span><span class="WHIT">
<span class='line'> 69</span> </span><span class="COMM">//	console.log(guille.name);</span><span class="WHIT">
<span class='line'> 70</span> 
<span class='line'> 71</span> </span><span class="WHIT">	</span><span class="NAME">Story.findOne</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 72</span> </span><span class="WHIT">		</span><span class="NAME">title</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="REGX">/Nintendo/i</span><span class="WHIT">
<span class='line'> 73</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">story</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 74</span> </span><span class="WHIT">		</span><span class="NAME">story._creator</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">guille</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="COMM">// or guille._id</span><span class="WHIT">
<span class='line'> 75</span> 
<span class='line'> 76</span> </span><span class="WHIT">		</span><span class="NAME">story.save</span><span class="PUNC">(</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">err</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 77</span> </span><span class="WHIT">				</span><span class="KEYW">if</span><span class="PUNC">(</span><span class="NAME">err</span><span class="PUNC">)</span><span class="WHIT"> 
<span class='line'> 78</span> 			</span><span class="NAME">Story.findOne</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 79</span> </span><span class="WHIT">				</span><span class="NAME">title</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="REGX">/Nintendo/i</span><span class="WHIT">
<span class='line'> 80</span> </span><span class="WHIT">			</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">populate</span><span class="PUNC">(</span><span class="STRN">'_creator'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="WHIT"> </span><span class="STRN">'name'</span><span class="WHIT"> </span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">.</span><span class="NAME">run</span><span class="PUNC">(</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">err</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">story</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 81</span> 
<span class='line'> 82</span> </span><span class="WHIT">				</span><span class="NAME">console.log</span><span class="PUNC">(</span><span class="STRN">'The creator is '</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">story._creator.name</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 83</span> </span><span class="WHIT">				</span><span class="COMM">// prints "The creator is Guillermo"</span><span class="WHIT">
<span class='line'> 84</span> </span><span class="WHIT">			</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 85</span> </span><span class="WHIT">		</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 86</span> </span><span class="WHIT">	</span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 87</span> </span><span class="COMM">//});</span><span class="WHIT">
<span class='line'> 88</span> </span></pre></body></html>